package com.cxs.xiaomi.web.controller;

import com.cxs.xiaomi.domain.User;
import com.cxs.xiaomi.service.UserService;
import com.cxs.xiaomi.service.impl.UserServiceImpl;
import com.cxs.xiaomi.web.controller.BaseServlet;
import cxs.servlet.exception.ParamMapException;
import cxs.servlet.exception.ParamValueException;
import cxs.utils.ParamsAsBean;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/*
 * @Author:cxs
 * @Date:2021/9/15  17:40
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@WebServlet(name = "AdminServlet", urlPatterns = "/admin")
public class AdminServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();

    public String adminLogin(HttpServletRequest request, HttpServletResponse response) {
        try {
            User user = ParamsAsBean.parammeterAsBean(User.class, request, false);
            if (user != null) {
                User adminUser = userService.adminLogin(user);
                if(adminUser != null){
                    // 登录成功
                    HttpSession session = request.getSession();
                    session.setAttribute("admin",adminUser);
                    return "redirect:/admin/admin.jsp";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/admin/login.jsp";
    }
}
